#coding=utf-8?
from docx import Document
from docx.shared import Inches
import os
from mailmerge import MailMerge

from .PeiYang import PeiYang

from .Matrix import Matrix
from .Supporting_Course import  Supporting_Course
# 创建新的word文档
def funOpenNewFile():
    document = Document()
    document.add_heading('hello word!', 0)
    document.save('hello.docx')

def AddImage():

    document = Document()
    document.add_heading('Document Title', 0)

    p = document.add_paragraph('A plain paragraph having some ')
    p.add_run('bold').bold = True
    p.add_run(' and some ')
    p.add_run('italic.').italic = True

    document.add_heading('Heading, level 1', level=1)
    document.add_paragraph('Intense quote', style='Intense Quote')

    document.add_paragraph(
        'first item in unordered list', style='List Bullet'
    )
    document.add_paragraph(
        'first item in ordered list', style='List Number'
    )

    document.add_picture('1.png', width=Inches(1.25))

    records = (
        (3, '101', 'Spam'),
        (7, '422', 'Eggs'),
        (4, '631', 'Spam, spam, eggs, and spam')
    )

    table = document.add_table(rows=1, cols=3)
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = 'Qty'
    hdr_cells[1].text = 'Id'
    hdr_cells[2].text = 'Desc'
    for qty, id, desc in records:
        row_cells = table.add_row().cells
        row_cells[0].text = str(qty)
        row_cells[1].text = id
        row_cells[2].text = desc

    document.add_page_break()

    document.save('img.docx')

def findPara(doc, text):
    target = None
    for paragraph in doc.paragraphs:
        paragraph_text = paragraph.text
        if paragraph_text.endswith(text):
            target = paragraph
            break
    return target

def testTemp():
    temp = MailMerge('temp.docx')
    temp.merge(目标="1、啊啊啊")
    temp.merge(预期="1、啊啊啊；\r\n2、不不不不不不不")
    temp.write('test.docx')

def merge():

    import csv

    template = 'template\湘南学院\计算机科学与技术\\在校学生证明模板.docx'

    with open('students.csv', encoding='utf-8')as f:
        f_csv = csv.reader(f)
        for row in f_csv:
            print(row)
            document = MailMerge(template)

            document.merge(name=row[0],
                           id=row[1],
                           major=row[2],
                           date=doc)
            document.write('out/证明_'+row[0]+'.docx')

def main(path,uerid,year):
    peiyang = PeiYang(path)
    # peiyang.LoadCourse()
    peiyang.word_y=year
    peiyang.LoadCousersFromDB(uerid,year)
    peiyang.showSummit()
    from .WordDBUse import DbUse
    wd = DbUse()
    wd.initdata()
    userdictinfo = wd.selectuserdictinfo(uerid)
    peiyang.writeGradeSummit('2022', userdictinfo["major"])

    peiyang.writeCoueseTables(["通识课", "学科基础课", "学科专业基础课", "专业选修课", "集中性实践"])

    peiyang.writeCourseMatrixSimple()

    peiyang.writeTarget()
    peiyang.writeWord()

    wd.userid=uerid

    matrix = Matrix(path, str(uerid))

    matrix.Get_num_colnum_dict()

    # 存储每个工作表的数据到course_content中
    matrix.GetCourseData(year)
    print(matrix.course_content)
    data = matrix.course_content
    wd.inserMatrix(data, year)




    return peiyang.semester

def Find_semester(path,userid,year):
    peiyang= PeiYang(path)
    peiyang.word_y = year
    peiyang.LoadCousersFromDB(userid,year)
    return peiyang.semester

def Find_Course_proportions(path,uerid,year):

    peiyang = PeiYang(path)
    peiyang.word_y = year
    peiyang.userid = uerid
    # peiyang.LoadCourse()
    peiyang.LoadCousersFromDB(uerid,year)
    return peiyang.Course_proportions


def Semester_courses(path,uerid,year):

    peiyang = PeiYang(path)
    peiyang.word_y = year
    peiyang.userid = uerid
    # peiyang.LoadCourse()
    peiyang.LoadCousersFromDB(uerid,year)
    return peiyang.semester

def Course_proportions(path,uerid,year):
    peiyang = PeiYang(path)
    peiyang.word_y = year
    peiyang.userid = uerid
    # peiyang.LoadCourse()
    peiyang.LoadCousersFromDB(uerid,year)
    return peiyang.Course_proportions

def Create_Matrix(path,uerid,year):
    matrix = Matrix(path,uerid)
    # 生成列名字典
    matrix.Get_num_colnum_dict()

    # 存储每个工作表的数据到course_content中
    matrix.GetCourseData(year)

    # 求二级指标点个数
    matrix.Get_index_point()

    # 添加数据到矩阵中
    matrix.AddContent()

    # 合并第一行的单元格
    matrix.MergeCell()

    # 设置文字居中
    matrix.TextCentered()
    matrix.SetCellSize()
    matrix.style_excel(matrix.ws)
    matrix.wb.save(matrix.path + matrix.useid + str(year)+ "Matrix.xlsx")
    return (matrix.path + matrix.useid + str(year)+"Matrix.xlsx")

def Create_Supporting_Course(path,useid,major,year):
    sup_cou = Supporting_Course(path, useid)
    # 生成矩阵表，拉获取其中的数据
    sup_cou.init(year)
    # 生成主要支撑课程的字典
    sup_cou.get_support_dict()
    # 生成毕业要求和指标点的内容
    sup_cou.get_data()
    # 生成标题
    sup_cou.set_title(major,year)
    # 创建表格
    sup_cou.create_table()
    # 填充表格内容
    sup_cou.fill_content()
    sup_cou.doc.save(sup_cou.path+sup_cou.uerid+str(year)+"Supporting_Course.docx")
    return (sup_cou.path+sup_cou.uerid+str(year)+"Supporting_Course.docx")




if __name__ == '__main__':
    main('template/湘南学院/计算机科学与技术/')

    pass
    #funOpenNewFile()
    #AddImage()
    #merge()
    # testTemp()


